package com.techsoul.module.pms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.constant.ApiConstants;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.NumberUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.basic.mapper.HrEmployeeManageMapper;
import com.techsoul.module.basic.pojo.entity.HrEmployeeManage;
import com.techsoul.module.basic.pojo.vo.HrEmployeeManageVo;
import com.techsoul.module.basic.service.IHrEmployeeManageService;
import com.techsoul.module.goods.pojo.vo.SkuVo;
import com.techsoul.module.pms.enmus.WmsInStatusEnum;
import com.techsoul.module.pms.enmus.WmsOutStatusEnum;
import com.techsoul.module.pms.mapper.*;
import com.techsoul.module.pms.pojo.entity.*;
import com.techsoul.module.pms.pojo.param.*;
import com.techsoul.module.pms.pojo.vo.*;
import com.techsoul.module.pms.service.ITWmsDeliveryPickupPlanService;
import com.techsoul.module.pms.service.ITbWmsMaterialDeliveryLineService;
import com.techsoul.module.pms.service.ITbWmsMaterialReceiptLineService;
import com.techsoul.module.system.enmus.UserType;
import com.techsoul.module.system.mapper.ConfigMapper;
import com.techsoul.module.system.pojo.entity.Config;
import com.techsoul.module.system.pojo.vo.ConfigVo;
import com.techsoul.module.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 拣货计划 Service实现
 *
 * @author liuen
 * @date 2023-03-28
 */
@Service
@Transactional(readOnly = true)
public class TWmsDeliveryPickupPlanServiceImpl implements ITWmsDeliveryPickupPlanService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TWmsDeliveryPickupPlanMapper mapper;
    @Autowired
    private HrEmployeeManageMapper hrEmployeeManageMapper;
    @Autowired
    private TbWmsMaterialDeliveryLineMapper tbWmsMaterialDeliveryLineMapper;
    @Autowired
    private TbWmsLocationMapper tbWmsLocationMapper;

    @Autowired
    private TWhWarehouseCustomerMaterialMapper tWhWarehouseCustomerMaterialMapper;
    @Autowired
    private TbWmsMaterialInLocationMapper tbWmsMaterialInLocationMapper;
    @Autowired
    private ITbWmsMaterialDeliveryLineService iTbWmsMaterialDeliveryLineService;

    @Autowired
    private ITbWmsMaterialReceiptLineService iTbWmsMaterialReceiptLineService;

    @Autowired
    private IHrEmployeeManageService iHrEmployeeManageService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ConfigMapper configMapper;
    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(TWmsDeliveryPickupPlanSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TWmsDeliveryPickupPlan entity = new TWmsDeliveryPickupPlan();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }
    @Transactional(readOnly = false)
    public void getPickUpPlans(List<TbWmsMaterialDeliveryLine> needPickUps,int seq,UserVo user){
        String maxload = this.configMapper.findByConfigKey("sys.max.load");
        int allsize= needPickUps.size();
        if(seq==1){
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperator(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperator(UserType.lift.getEventCode());
           int needs = needPickUps.size();
            needs = needs--;
            if(pickUpUsers.size()>needPickUps.size()){
                pickUpUsers = pickUpUsers.subList(0,needs);
            }
            if(liftUpUsers.size()>needPickUps.size()){
                liftUpUsers = liftUpUsers.subList(0,needs);
            }
            int pickuserIndex = 0;
            int liftuserIndex = 0;
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0&&liftUpUsers!=null&&liftUpUsers.size()>0){
                int startposition = 0;
                int startLiftposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));
                Long jobsLiftPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),liftUpUsers.size()));
                if(jobsPerpersonEst>Long.valueOf(maxload)){
                    jobsPerpersonEst = Long.valueOf(maxload);

                }
                if(jobsLiftPerpersonEst>Long.valueOf(maxload)){
                    jobsLiftPerpersonEst = Long.valueOf(maxload);

                }
                int endposition = jobsPerpersonEst.intValue();
                int endLiftposition = jobsLiftPerpersonEst.intValue();
                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                  //  HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps);
                    }
                    if(endposition>=needPickUps.size()){
                        this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,needPickUps.size()-1),
                                user,UserType.pick.getEventCode());
                        break;
                    }else {
                        this.createDispatch(dispatchuserVo, needPickUps.subList(startposition, endposition),
                                user, UserType.pick.getEventCode());
                        //  this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),
                        //     user,UserType.lift.getEventCode());
                        startposition = endposition + 1;
                        endposition = endposition + jobsPerpersonEst.intValue();
                    }


                }
                for(int i=0;i<liftUpUsers.size();i++){
                  //  HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);

                    //   if(i==pickUpUsers.size()-1){
                    //         this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,pickUpUsers.size()));
                    //   }else{
                    if(endLiftposition>=needPickUps.size()){
                        this.createDispatch(forkliftuserVo,needPickUps.subList(startLiftposition,needPickUps.size()-1),
                                user,UserType.lift.getEventCode());
                      break;
                    }else{
                        this.createDispatch(forkliftuserVo,needPickUps.subList(startLiftposition,endLiftposition),
                                user,UserType.lift.getEventCode());
                        startLiftposition = endLiftposition+1;
                        endLiftposition = endLiftposition+jobsLiftPerpersonEst.intValue();
                    }

                    //    }


                }


                if(jobsPerpersonEst>Long.valueOf(maxload)){
                    List<TbWmsMaterialDeliveryLine> needPickUpsLeft = needPickUps.subList(endposition,allsize);
                    seq = seq+1;
                    this.getPickUpPlans(needPickUpsLeft, seq,user);
                }
            }  else{
                seq = seq+1;
                this.getPickUpPlans(needPickUps, seq,user);

            }
        }
        else if(seq==2){
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.lift.getEventCode());

            Double newmax = NumberUtils.mul(Double.valueOf(maxload),0.70);
            maxload = String.valueOf(newmax);
            int needs = needPickUps.size();
            needs = needs--;
            if(pickUpUsers.size()>needPickUps.size()){
                pickUpUsers = pickUpUsers.subList(0,needs);
            }
            if(liftUpUsers.size()>needPickUps.size()){
                liftUpUsers = liftUpUsers.subList(0,needs);
            }
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0&&
                    liftUpUsers!=null&&liftUpUsers.size()>0){
                int startposition = 0;
                int startLiftposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));
                Long jobsLiftPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),liftUpUsers.size()));
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    jobsPerpersonEst = Double.valueOf(maxload).longValue();

                }
                if(jobsLiftPerpersonEst>Double.valueOf(maxload)){
                    jobsLiftPerpersonEst = Double.valueOf(maxload).longValue();

                }
                int endposition = jobsLiftPerpersonEst.intValue();
                int endLiftposition = jobsLiftPerpersonEst.intValue();
                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                 //   HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps);
                    }
                    if(endposition>=needPickUps.size()){
                        this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,needPickUps.size()-1),
                                user,UserType.pick.getEventCode());
                        break;
                    }else {
                        this.createDispatch(dispatchuserVo, needPickUps.subList(startposition, endposition),
                                user, UserType.pick.getEventCode());
                        //    this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),user,
                        //          UserType.lift.getEventCode());

                        startposition = endposition + 1;
                        endposition = endposition + jobsPerpersonEst.intValue();
                    }
                }
                for(int i=0;i<liftUpUsers.size();i++){
                 //   HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);

                    if(endLiftposition>=needPickUps.size()){
                        this.createDispatch(forkliftuserVo,needPickUps.subList(startLiftposition,needPickUps.size()-1),
                                user,UserType.lift.getEventCode());
                        break;
                    }else {
                        this.createDispatch(forkliftuserVo, needPickUps.subList(startLiftposition, endLiftposition), user,
                                UserType.lift.getEventCode());

                        startLiftposition = endLiftposition + 1;
                        endLiftposition = endLiftposition + jobsLiftPerpersonEst.intValue();
                    }
                }
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    List<TbWmsMaterialDeliveryLine> needPickUpsLeft = needPickUps.subList(endposition,allsize);
                    seq = seq+1;
                    this.getPickUpPlans(needPickUpsLeft, seq,user);
                }
            }else{
                seq = seq+1;
                this.getPickUpPlans(needPickUps, seq,user);
            }

        } else if(seq==3){
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperatorSixtyLoad(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.lift.getEventCode());

            Double newmax = NumberUtils.mul(Double.valueOf(maxload),0.40);
            maxload = String.valueOf(newmax);
            int needs = needPickUps.size();
            needs = needs--;
            if(pickUpUsers.size()>needPickUps.size()){
                pickUpUsers = pickUpUsers.subList(0,needs);
            }
            if(liftUpUsers.size()>needPickUps.size()){
                liftUpUsers = liftUpUsers.subList(0,needs);
            }
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0
                    &&liftUpUsers!=null&&liftUpUsers.size()>0){

                int startposition = 0;
                int startLiftposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));
                Long jobsLiftPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),liftUpUsers.size()));
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    jobsPerpersonEst = Double.valueOf(maxload).longValue();

                }
                if(jobsLiftPerpersonEst>Double.valueOf(maxload)){
                    jobsLiftPerpersonEst = Double.valueOf(maxload).longValue();

                }
                int endposition = jobsPerpersonEst.intValue();
                int endLiftposition = jobsLiftPerpersonEst.intValue();
                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                 //   HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps);
                    }
                    if(endposition>=needPickUps.size()){
                        this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,needPickUps.size()-1),
                                user,UserType.pick.getEventCode());
                        break;
                    }else {
                        this.createDispatch(dispatchuserVo, needPickUps.subList(startposition, endposition), user, UserType.pick.getEventCode());
                        //   this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),user,UserType.lift.getEventCode());

                        startposition = endposition + 1;
                        endposition = endposition + jobsPerpersonEst.intValue();
                    }
                }
                for(int i=0;i<liftUpUsers.size();i++){
               //     HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);

                   // this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,endposition),user,UserType.pick.getEventCode());
                    if(endLiftposition>=needPickUps.size()){
                        this.createDispatch(forkliftuserVo,needPickUps.subList(startLiftposition,needPickUps.size()-1),
                                user,UserType.lift.getEventCode());
                        break;
                    }else {
                    this.createDispatch(forkliftuserVo,needPickUps.subList(startLiftposition,endLiftposition),user,UserType.lift.getEventCode());

                    startLiftposition = endLiftposition+1;
                    endLiftposition = endLiftposition+jobsLiftPerpersonEst.intValue();
                    }
                }
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    List<TbWmsMaterialDeliveryLine> needPickUpsLeft = needPickUps.subList(endposition,allsize);
                    seq = seq+1;
                    this.getPickUpPlans(needPickUpsLeft, seq,user);
                }
            }else{
                seq = seq+1;
                this.getPickUpPlans(needPickUps, seq,user);
            }
        } else{
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperatorFullLoad(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.lift.getEventCode());
            int needs = needPickUps.size();
            needs = needs--;
            if(pickUpUsers.size()>needPickUps.size()){
                pickUpUsers = pickUpUsers.subList(0,needs);
            }
            if(liftUpUsers.size()>needPickUps.size()){
                liftUpUsers = liftUpUsers.subList(0,needs);
            }
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0&&
                    liftUpUsers!=null&&liftUpUsers.size()>0){

                int startposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));
                int startLiftposition = 0;
                Long jobsLiftPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),liftUpUsers.size()));
                int endposition = jobsPerpersonEst.intValue();
                int endLiftposition = jobsLiftPerpersonEst.intValue();
                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                 //   HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps );
                    }
                    if(endposition>=needPickUps.size()){
                        this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,needPickUps.size()-1),
                                user,UserType.pick.getEventCode());
                        break;
                    }else {
                        this.createDispatch(dispatchuserVo, needPickUps.subList(startposition, endposition), user, UserType.pick.getEventCode());
                        //  this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),user,UserType.lift.getEventCode());

                        startposition = endposition + 1;
                        endposition = endposition + jobsPerpersonEst.intValue();
                    }
                }
                for(int i=0;i<liftUpUsers.size();i++){
                 //   HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(endLiftposition>=needPickUps.size()){
                        this.createDispatch(forkliftuserVo,needPickUps.subList(startLiftposition,needPickUps.size()-1),
                                user,UserType.lift.getEventCode());
                        break;
                    }else {
                        this.createDispatch(forkliftuserVo, needPickUps.subList(startLiftposition, endLiftposition), user, UserType.lift.getEventCode());

                        startLiftposition = endLiftposition + 1;
                        endLiftposition = endLiftposition + jobsLiftPerpersonEst.intValue();
                    }
                }
            }
        }

    }
    @Transactional(readOnly = false)
    @Override
    public String handlePublish() {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        TbWmsMaterialDeliveryLineSearchParam tbWmsMatterialDeliveryLineSearchParam
                = new TbWmsMaterialDeliveryLineSearchParam();
        tbWmsMatterialDeliveryLineSearchParam.setStatus(WmsInStatusEnum.created.getValue());
        List<TbWmsMaterialDeliveryLine> thisneedDeliverys =
                this.iTbWmsMaterialDeliveryLineService.findAllOrderByMaterial(tbWmsMatterialDeliveryLineSearchParam);
        List<TbWmsMaterialDeliveryLine> needDeliverys = new ArrayList<TbWmsMaterialDeliveryLine>();

        int seq = 1;
        for(int i=0;i<thisneedDeliverys.size();i++){
            TbWmsMaterialDeliveryLine tbWmsMaterialDeliveryLine
                    = (TbWmsMaterialDeliveryLine)thisneedDeliverys.get(i);
            Double addedQty = 0.0;
            String receptIds = " ";
            String locationNos = " ";
            if (tbWmsMaterialDeliveryLine.getInAndOutType() != null) {
                if (tbWmsMaterialDeliveryLine.getInAndOutType().equals("先进先出")) {
                    List<TbWmsMaterialInLocationVo> tbWmsMaterialInLocationVos =
                            this.tbWmsMaterialInLocationMapper.findMaterialByFifo(tbWmsMaterialDeliveryLine.getMaterialNo());
                    for (int j = 0; j < tbWmsMaterialInLocationVos.size(); j++) {
                        TbWmsMaterialInLocationVo tbWmsMaterialInLocationVo = (TbWmsMaterialInLocationVo) tbWmsMaterialInLocationVos.get(j);
                        addedQty = NumberUtils.add(addedQty, tbWmsMaterialInLocationVo.getQuantity());
                        receptIds = receptIds.concat(tbWmsMaterialInLocationVo.getReceivedLineId()).concat(";");
                        locationNos = locationNos.concat(tbWmsMaterialInLocationVo.getLocationNo()).concat(";");
                        if (addedQty >= tbWmsMaterialDeliveryLine.getQuantity()) {
                            break;
                        }
                    }
                } else {
                    List<TbWmsMaterialInLocation> tbWmsMaterialInLocationVos =
                            this.tbWmsMaterialInLocationMapper.findByMaterialNo(tbWmsMaterialDeliveryLine.getMaterialNo());
                    for (int j = 0; j < tbWmsMaterialInLocationVos.size(); j++) {
                        TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation) tbWmsMaterialInLocationVos.get(j);
                        addedQty = NumberUtils.add(addedQty, tbWmsMaterialInLocation.getQuantity());
                        receptIds = receptIds.concat(tbWmsMaterialInLocation.getReceivedLineId()).concat(";");
                        locationNos = locationNos.concat(tbWmsMaterialInLocation.getLocationNo()).concat(";");
                        if (addedQty >= tbWmsMaterialDeliveryLine.getQuantity()) {
                            break;
                        }
                    }
                }
            } else {
                List<TbWmsMaterialInLocation> tbWmsMaterialInLocationVos =
                        this.tbWmsMaterialInLocationMapper.findByMaterialNo(tbWmsMaterialDeliveryLine.getMaterialNo());
                for (int j = 0; j < tbWmsMaterialInLocationVos.size(); j++) {
                    TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation) tbWmsMaterialInLocationVos.get(j);
                    addedQty = NumberUtils.add(addedQty, tbWmsMaterialInLocation.getQuantity());
                    receptIds = receptIds.concat(tbWmsMaterialInLocation.getReceivedLineId()).concat(";");
                    locationNos = locationNos.concat(tbWmsMaterialInLocation.getLocationNo()).concat(";");
                    if (addedQty >= tbWmsMaterialDeliveryLine.getQuantity()) {
                        break;
                    }
                }
            }
            tbWmsMaterialDeliveryLine.setLocationNo(locationNos);
            tbWmsMaterialDeliveryLine.setReceivedLineId(receptIds);
            tbWmsMaterialDeliveryLine.setStatus(WmsOutStatusEnum.planed.getValue());

            tbWmsMaterialDeliveryLine.preUpdate(user.getId());

            this.tbWmsMaterialDeliveryLineMapper.updateById(tbWmsMaterialDeliveryLine);
            needDeliverys.add(tbWmsMaterialDeliveryLine);
        }
        return this.genPublishLocPlan(needDeliverys,seq,user);



    }

    @Transactional(readOnly = false)
    public String genPublishLocPlan(List<TbWmsMaterialDeliveryLine> needDeliverys ,int seq,UserVo userVo){
        String rt ="success";

        for(int i=0; i< needDeliverys.size();i++){
            TbWmsMaterialDeliveryLine tbWmsMaterialDeliveryLine = (TbWmsMaterialDeliveryLine)needDeliverys.get(i);
            TWhWarehouseCustomerMaterial tWhWarehouseCustomerMaterial = this.tWhWarehouseCustomerMaterialMapper.findBySkuCode(tbWmsMaterialDeliveryLine.getSkuCode());


            List<TbWmsMaterialDeliveryLine> thislist = new ArrayList<TbWmsMaterialDeliveryLine>();
            thislist.add(tbWmsMaterialDeliveryLine);
            this.createDispatch(null,thislist,userVo,UserType.pick.getEventCode());
            if(tWhWarehouseCustomerMaterial.getNeedLiftYn()!=null){

                this.createDispatch(null,thislist,userVo,UserType.lift.getEventCode());
            }


        }

        return rt;
    }

    @Transactional(readOnly = false)
    public void getPickUpPlans(TWmsDeliveryPickupPlanSaveParam param,List<TbWmsMaterialDeliveryLine> needPickUps,int seq,UserVo user){
        String maxload = this.configMapper.findByConfigKey("sys.max.load");
        int allsize= needPickUps.size();
        if(seq==1){
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperator(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperator(UserType.lift.getEventCode());

            int pickuserIndex = 0;
            int liftuserIndex = 0;
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0){
                int startposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));
                if(jobsPerpersonEst>Long.valueOf(maxload)){
                    jobsPerpersonEst = Long.valueOf(maxload);

                }
                int endposition = jobsPerpersonEst.intValue();

                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps);
                    }
                    //   if(i==pickUpUsers.size()-1){
                    //         this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,pickUpUsers.size()));
                    //   }else{
                    this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,endposition),
                            user,UserType.pick.getEventCode());
                    this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),
                            user,UserType.lift.getEventCode());
                    startposition = endposition+1;
                    endposition = endposition+jobsPerpersonEst.intValue();
                    //    }


                }
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    List<TbWmsMaterialDeliveryLine> needPickUpsLeft = needPickUps.subList(endposition,allsize);
                    seq = seq+1;
                    this.getPickUpPlans(param,needPickUpsLeft, seq,user);
                }
            }  else{
                seq = seq+1;
                this.getPickUpPlans(param,needPickUps, seq,user);

            }
        }  else if(seq==2){
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.lift.getEventCode());

            Double newmax = NumberUtils.mul(Double.valueOf(maxload),0.70);
            maxload = String.valueOf(newmax);
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0){
                int startposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    jobsPerpersonEst = Double.valueOf(maxload).longValue();

                }
                int endposition = jobsPerpersonEst.intValue();
                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps);
                    }
                    this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,endposition),
                            user,UserType.pick.getEventCode());
                    this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),user,
                            UserType.lift.getEventCode());

                    startposition = endposition+1;
                    endposition = endposition+jobsPerpersonEst.intValue();
                }
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    List<TbWmsMaterialDeliveryLine> needPickUpsLeft = needPickUps.subList(endposition,allsize);
                    seq = seq+1;
                    this.getPickUpPlans(param,needPickUpsLeft, seq,user);
                }
            }else{
                seq = seq+1;
                this.getPickUpPlans(param,needPickUps, seq,user);
            }

        } else if(seq==3){
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperatorSixtyLoad(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.lift.getEventCode());

            Double newmax = NumberUtils.mul(Double.valueOf(maxload),0.40);
            maxload = String.valueOf(newmax);
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0){
                int startposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    jobsPerpersonEst = Double.valueOf(maxload).longValue();

                }

                int endposition = jobsPerpersonEst.intValue();
                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps);
                    }
                    this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,endposition),user,UserType.lift.getEventCode());
                    this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),user,UserType.lift.getEventCode());

                    startposition = endposition+1;
                    endposition = endposition+jobsPerpersonEst.intValue();
                }
                if(jobsPerpersonEst>Double.valueOf(maxload)){
                    List<TbWmsMaterialDeliveryLine> needPickUpsLeft = needPickUps.subList(endposition,allsize);
                    seq = seq+1;
                    this.getPickUpPlans(param,needPickUpsLeft, seq,user);
                }
            }else{
                seq = seq+1;
                this.getPickUpPlans(param,needPickUps, seq,user);
            }
        } else{
            List<HrEmployeeManageVo> pickUpUsers = this.iHrEmployeeManageService.findOperatorFullLoad(UserType.pick.getEventCode());
            List<HrEmployeeManageVo> liftUpUsers = this.iHrEmployeeManageService.findOperatorThirtyLoad(UserType.lift.getEventCode());
            if(needPickUps!=null&&needPickUps.size()>0&&pickUpUsers!=null&&pickUpUsers.size()>0){
                int startposition = 0;
                Long jobsPerpersonEst = Math.round(NumberUtils.div(needPickUps.size(),pickUpUsers.size()));

                int endposition = jobsPerpersonEst.intValue();
                for(int i=0;i<pickUpUsers.size();i++){
                    HrEmployeeManageVo dispatchuserVo = (HrEmployeeManageVo)pickUpUsers.get(i);
                    HrEmployeeManageVo forkliftuserVo = (HrEmployeeManageVo)liftUpUsers.get(i);
                    if(i==0){
                        this.createPackingContainer(dispatchuserVo,needPickUps );
                    }
                    this.createDispatch(dispatchuserVo,needPickUps.subList(startposition,endposition),user,UserType.pick.getEventCode());
                    this.createDispatch(forkliftuserVo,needPickUps.subList(startposition,endposition),user,UserType.lift.getEventCode());

                    startposition = endposition+1;
                    endposition = endposition+jobsPerpersonEst.intValue();
                }

            }
        }

    }



    @Transactional(readOnly = false)
    @Override
    public int getPickUpJob(String id){
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        HrEmployeeManageVo dispatchuserVo = this.hrEmployeeManageMapper.findByUserId(user.getId());
        TWmsDeliveryPickupPlan tWmsDeliveryPickupPlan = this.mapper.selectById(id);
        tWmsDeliveryPickupPlan.setPickUpUserName(dispatchuserVo.getPersonName());
        tWmsDeliveryPickupPlan.setPickUpUserNo(dispatchuserVo.getId());
        tWmsDeliveryPickupPlan.setStatus("created");
        tWmsDeliveryPickupPlan.preUpdate(user.getId());
        return this.mapper.updateById(tWmsDeliveryPickupPlan);
    }

    @Transactional(readOnly = false)
    @Override
    public String genPickupPlanBySelectItem(TWmsDeliveryPickupPlanSaveParam params, String [] ids){
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<TbWmsMaterialDeliveryLine> needPickUps =new ArrayList<TbWmsMaterialDeliveryLine>();
        for(int i=0;i<ids.length;i++){
            TbWmsMaterialDeliveryLine tbWmsMaterialDeliveryLine
                    = this.tbWmsMaterialDeliveryLineMapper.selectById(ids[i]);
            Double addedQty = 0.0;
            String receptIds = " ";
            String locationNos = " ";
            if(tbWmsMaterialDeliveryLine.getInAndOutType()!=null) {
                if (tbWmsMaterialDeliveryLine.getInAndOutType().equals("先进先出")) {
                    List<TbWmsMaterialInLocationVo> tbWmsMaterialInLocationVos =
                            this.tbWmsMaterialInLocationMapper.findMaterialByFifo(tbWmsMaterialDeliveryLine.getMaterialNo());
                    for (int j = 0; j < tbWmsMaterialInLocationVos.size(); j++) {
                        TbWmsMaterialInLocationVo tbWmsMaterialInLocationVo = (TbWmsMaterialInLocationVo) tbWmsMaterialInLocationVos.get(j);
                        addedQty = NumberUtils.add(addedQty, tbWmsMaterialInLocationVo.getQuantity());
                        receptIds = receptIds.concat(tbWmsMaterialInLocationVo.getReceivedLineId()).concat(";");
                        locationNos = locationNos.concat(tbWmsMaterialInLocationVo.getLocationNo()).concat(";");
                        if (addedQty >= tbWmsMaterialDeliveryLine.getQuantity()) {
                            break;
                        }
                    }
                }else{
                    List<TbWmsMaterialInLocation> tbWmsMaterialInLocationVos =
                            this.tbWmsMaterialInLocationMapper.findByMaterialNo(tbWmsMaterialDeliveryLine.getMaterialNo());
                    for(int j=0; j <tbWmsMaterialInLocationVos.size();j++){
                        TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation)tbWmsMaterialInLocationVos.get(j);
                        addedQty = NumberUtils.add(addedQty,tbWmsMaterialInLocation.getQuantity());
                        receptIds = receptIds.concat(tbWmsMaterialInLocation.getReceivedLineId()).concat(";");
                        locationNos = locationNos.concat(tbWmsMaterialInLocation.getLocationNo()).concat(";");
                        if(addedQty>=tbWmsMaterialDeliveryLine.getQuantity()){
                            break;
                        }
                    }
                }
            }else{
                List<TbWmsMaterialInLocation> tbWmsMaterialInLocationVos =
                        this.tbWmsMaterialInLocationMapper.findByMaterialNo(tbWmsMaterialDeliveryLine.getMaterialNo());
                for(int j=0; j <tbWmsMaterialInLocationVos.size();j++){
                    TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation)tbWmsMaterialInLocationVos.get(j);
                    addedQty = NumberUtils.add(addedQty,tbWmsMaterialInLocation.getQuantity());
                    receptIds = receptIds.concat(tbWmsMaterialInLocation.getReceivedLineId()).concat(";");
                    locationNos = locationNos.concat(tbWmsMaterialInLocation.getLocationNo()).concat(";");
                    if(addedQty>=tbWmsMaterialDeliveryLine.getQuantity()){
                        break;
                    }
                }
            }
            tbWmsMaterialDeliveryLine.setLocationNo(locationNos);
            tbWmsMaterialDeliveryLine.setReceivedLineId(receptIds);
            tbWmsMaterialDeliveryLine.setStatus(WmsOutStatusEnum.planed.getValue());

            tbWmsMaterialDeliveryLine.preUpdate(user.getId());

            this.tbWmsMaterialDeliveryLineMapper.updateById(tbWmsMaterialDeliveryLine);
            needPickUps.add(tbWmsMaterialDeliveryLine);
        }


        int seq = 1;


        this.getPickUpPlans(params,needPickUps,seq,user);

        return "success";
    }
    @Transactional(readOnly = false)
    @Override
    public String genPickupPlan() {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        //一样的地方要分给同一人
        //

        TbWmsMaterialDeliveryLineSearchParam tbWmsMaterialDeliveryLineSearchParam
                = new TbWmsMaterialDeliveryLineSearchParam();
        tbWmsMaterialDeliveryLineSearchParam.setStatus(WmsOutStatusEnum.created.getValue());
        List<TbWmsMaterialDeliveryLine> needPickUps = new ArrayList<TbWmsMaterialDeliveryLine>();
        List<TbWmsMaterialDeliveryLine> thisneedPickUps = this.iTbWmsMaterialDeliveryLineService.findAllOrderByMaterial(tbWmsMaterialDeliveryLineSearchParam);
        for(int i=0;i<thisneedPickUps.size();i++){
            TbWmsMaterialDeliveryLine tbWmsMaterialDeliveryLine
                    = (TbWmsMaterialDeliveryLine)thisneedPickUps.get(i);
            Double addedQty = 0.0;
            String receptIds = " ";
            String locationNos = " ";
            if (tbWmsMaterialDeliveryLine.getInAndOutType() != null) {
                if (tbWmsMaterialDeliveryLine.getInAndOutType().equals("先进先出")) {
                    List<TbWmsMaterialInLocationVo> tbWmsMaterialInLocationVos =
                            this.tbWmsMaterialInLocationMapper.findMaterialByFifo(tbWmsMaterialDeliveryLine.getMaterialNo());
                    for (int j = 0; j < tbWmsMaterialInLocationVos.size(); j++) {
                        TbWmsMaterialInLocationVo tbWmsMaterialInLocationVo = (TbWmsMaterialInLocationVo) tbWmsMaterialInLocationVos.get(j);
                        addedQty = NumberUtils.add(addedQty, tbWmsMaterialInLocationVo.getQuantity());
                        receptIds = receptIds.concat(tbWmsMaterialInLocationVo.getReceivedLineId()).concat(";");
                        locationNos = locationNos.concat(tbWmsMaterialInLocationVo.getLocationNo()).concat(";");
                        if (addedQty >= tbWmsMaterialDeliveryLine.getQuantity()) {
                            break;
                        }
                    }
                } else {
                    List<TbWmsMaterialInLocation> tbWmsMaterialInLocationVos =
                            this.tbWmsMaterialInLocationMapper.findByMaterialNo(tbWmsMaterialDeliveryLine.getMaterialNo());
                    for (int j = 0; j < tbWmsMaterialInLocationVos.size(); j++) {
                        TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation) tbWmsMaterialInLocationVos.get(j);
                        addedQty = NumberUtils.add(addedQty, tbWmsMaterialInLocation.getQuantity());
                        receptIds = receptIds.concat(tbWmsMaterialInLocation.getReceivedLineId()).concat(";");
                        locationNos = locationNos.concat(tbWmsMaterialInLocation.getLocationNo()).concat(";");
                        if (addedQty >= tbWmsMaterialDeliveryLine.getQuantity()) {
                            break;
                        }
                    }
                }
            } else {
                List<TbWmsMaterialInLocation> tbWmsMaterialInLocationVos =
                        this.tbWmsMaterialInLocationMapper.findByMaterialNo(tbWmsMaterialDeliveryLine.getMaterialNo());
                for (int j = 0; j < tbWmsMaterialInLocationVos.size(); j++) {
                    TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation) tbWmsMaterialInLocationVos.get(j);
                    addedQty = NumberUtils.add(addedQty, tbWmsMaterialInLocation.getQuantity());
                    receptIds = receptIds.concat(tbWmsMaterialInLocation.getReceivedLineId()).concat(";");
                    locationNos = locationNos.concat(tbWmsMaterialInLocation.getLocationNo()).concat(";");
                    if (addedQty >= tbWmsMaterialDeliveryLine.getQuantity()) {
                        break;
                    }
                }
            }
            tbWmsMaterialDeliveryLine.setLocationNo(locationNos);
            tbWmsMaterialDeliveryLine.setReceivedLineId(receptIds);
            tbWmsMaterialDeliveryLine.setStatus(WmsOutStatusEnum.planed.getValue());

            tbWmsMaterialDeliveryLine.preUpdate(user.getId());

            this.tbWmsMaterialDeliveryLineMapper.updateById(tbWmsMaterialDeliveryLine);
            needPickUps.add(tbWmsMaterialDeliveryLine);
        }
        int seq = 1;


        this.getPickUpPlans(needPickUps,seq,user);

        return "success";
    }
    @Transactional(readOnly = false)
    @Override
    public void createDispatch(HrEmployeeManageVo dispatchuserVo,List<TbWmsMaterialDeliveryLine> needPickUps,UserVo user,String type){
        //add createDispatch
        double thistimejob = 0.0;
        for(int i =0; i<needPickUps.size(); i++){
            TbWmsMaterialDeliveryLine tbWmsMaterialDeliveryLineVo = (TbWmsMaterialDeliveryLine)needPickUps.get(i);
            TWmsDeliveryPickupPlan tWmsDeliveryPickupPlan = new TWmsDeliveryPickupPlan();
            tWmsDeliveryPickupPlan.setCustomerName(tbWmsMaterialDeliveryLineVo.getCustomerName());
            tWmsDeliveryPickupPlan.setDeliveryLineId(tbWmsMaterialDeliveryLineVo.getId());
            tWmsDeliveryPickupPlan.setLocationNo(tbWmsMaterialDeliveryLineVo.getLocationNo());
            tWmsDeliveryPickupPlan.setMaterialName(tbWmsMaterialDeliveryLineVo.getMaterialName());
            tWmsDeliveryPickupPlan.setQty(tWmsDeliveryPickupPlan.getQty());
            tWmsDeliveryPickupPlan.setSeqNo(new Long(i));
            tWmsDeliveryPickupPlan.setReciptLineNo(tbWmsMaterialDeliveryLineVo.getReceivedLineId());
            tWmsDeliveryPickupPlan.setJobType(type);
            tWmsDeliveryPickupPlan.setRequstDeliveryDate(new Date());
            if(dispatchuserVo!=null){
                tWmsDeliveryPickupPlan.setPickUpUserName(dispatchuserVo.getPersonName());
                tWmsDeliveryPickupPlan.setPickUpUserNo(dispatchuserVo.getId());
                tWmsDeliveryPickupPlan.setStatus("created");
            }else{
                tWmsDeliveryPickupPlan.setPickUpUserName("任务发布");
                tWmsDeliveryPickupPlan.setPickUpUserNo("任务发布");
                tWmsDeliveryPickupPlan.setStatus("published");
            }

            tWmsDeliveryPickupPlan.setId(idWorker.nextId() + "").preInsert(user.getId());
            this.mapper.insert(tWmsDeliveryPickupPlan);
            thistimejob = NumberUtils.add(thistimejob,1.0);

        }
        if(dispatchuserVo!=null) {
            String maxload = this.configMapper.findByConfigKey("sys.max.load");
            double jobloads = dispatchuserVo.getCurrentJobLoad();
            double max = Double.valueOf(maxload);
            double thisload = NumberUtils.div(thistimejob, max);
            double nowload = NumberUtils.add(jobloads, thisload);

            HrEmployeeManage hrEmployeeManage = this.hrEmployeeManageMapper.selectById(dispatchuserVo.getId());
            hrEmployeeManage.setCurrentJobLoad(nowload);
            hrEmployeeManage.preUpdate(user.getId());
            this.hrEmployeeManageMapper.updateById(hrEmployeeManage);
        }
    }
    @Transactional(readOnly = false)
    @Override
    public void createPackingContainer(HrEmployeeManageVo dispatchuserVo,List<TbWmsMaterialDeliveryLine> lines){
        //add createPackingContainer job
    }






    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(TWmsDeliveryPickupPlanSaveParam params) {
        TWmsDeliveryPickupPlan entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }
    @Transactional(readOnly = false)
    @Override
    public Integer pickUpAndOutbound(TWmsDeliveryPickupPlanSaveParam params){
        List<String> idlist = params.getIds();
        List<TbWmsMaterialDeliveryLineSaveParam> locationObjs = new ArrayList<TbWmsMaterialDeliveryLineSaveParam>();
        List<String> locations = new ArrayList<String>();
        TbWmsLocation tbWmsLot = null;
        List<String> outids = new ArrayList<String>();
        String ids= "";
        for(int i= 0; i <idlist.size();i++){
            TWmsDeliveryPickupPlan entityFromDb = this.mapper.selectById((String)idlist.get(i));
            TbWmsMaterialDeliveryLine temParam= new TbWmsMaterialDeliveryLine();

            UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
            entityFromDb.setStatus(WmsOutStatusEnum.outed.getValue());
            entityFromDb.preUpdate(sysUser.getId());
             this.mapper.updateById(entityFromDb);
                temParam = this.tbWmsMaterialDeliveryLineMapper.selectById(entityFromDb.getDeliveryLineId());
                temParam.setStatus(WmsOutStatusEnum.outed.getValue());
                temParam.setContainerNo(params.getContianBoxNo());
                temParam.preUpdate(sysUser.getId());
                if(temParam.getVolume()==null){
                    TWhWarehouseCustomerMaterial tWhWarehouseCustomerMaterial
                            =this.tWhWarehouseCustomerMaterialMapper.findBySkuCode(temParam.getSkuCode());

                    Double volume=
                            NumberUtils.mul(tWhWarehouseCustomerMaterial.getUnitVolume().doubleValue(),temParam.getQuantity());
                    temParam.setVolume(new BigDecimal(volume));
                }
                this.tbWmsMaterialDeliveryLineMapper.updateById(temParam);
                outids.add(temParam.getId());
             //   this.iTbWmsMaterialDeliveryLineService.refreshDeliveryPlanHead(WmsOutStatusEnum.outed.getValue(),temParam.getDeliveryId());
                tbWmsLot =this.tbWmsLocationMapper.findByLocationNo(entityFromDb.getLocationNo());


                if(temParam.getVolume()!=null){
                    Double currentlocationload = tbWmsLot.getCurrentLoad();
                    if(currentlocationload!=null){
                        Double totload = NumberUtils.sub(currentlocationload,temParam.getVolume().doubleValue());
                        tbWmsLot.setCurrentLoad(totload);
                        tbWmsLot.preUpdate(sysUser.getId());
                        this.tbWmsLocationMapper.updateById(tbWmsLot);
                    }else{
                        tbWmsLot.setCurrentLoad(0.0);
                        tbWmsLot.preUpdate(sysUser.getId());
                        this.tbWmsLocationMapper.updateById(tbWmsLot);
                    }

                }


            List<TbWmsMaterialInLocation>  tbWmsMaterialInLocations  =
                    this.tbWmsMaterialInLocationMapper.findByReciptLineId(temParam.getReceivedLineId());
            if(tbWmsMaterialInLocations!=null&&tbWmsMaterialInLocations.size()>0){
                for(int j=0;j<tbWmsMaterialInLocations.size();j++){
                    TbWmsMaterialInLocation tbWmsMaterialInLocation =(TbWmsMaterialInLocation)tbWmsMaterialInLocations.get(j);
                    this.tbWmsMaterialInLocationMapper.deleteById(tbWmsMaterialInLocation.getId());
                }

            }

         /*   for(int j=0;j<tbWmsMaterialInLocations.size();j++){
                TbWmsMaterialInLocation tbWmsMaterialInLocation = (TbWmsMaterialInLocation)tbWmsMaterialInLocations.get(j);
                if(tbWmsMaterialInLocation.getQuantity().doubleValue()<=needoutNumber)
                {
                    this.tbWmsMaterialInLocationMapper.deleteById(tbWmsMaterialInLocation.getId());
                    needoutNumber = needoutNumber-tbWmsMaterialInLocation.getQuantity().doubleValue();
                    if(needoutNumber<=0){
                        break;
                    }
                }else{
                    Double nownum = NumberUtils.sub(tbWmsMaterialInLocation.getQuantity().doubleValue(),needoutNumber);
                    tbWmsMaterialInLocation.setQuantity(new BigDecimal(nownum));
                    tbWmsMaterialInLocation.preUpdate(sysUser.getId());
                    this.tbWmsMaterialInLocationMapper.updateById(tbWmsMaterialInLocation);

                    break;
                }
            }*/

        }
      /*  for(int j=0; j<locationObjs.size();j++){
            TbWmsMaterialDeliveryLineSaveParam tbWmsMaterialDeliveryLineSaveParam
                    =(TbWmsMaterialDeliveryLineSaveParam)locationObjs.get(j);
            this.autoWarehouseFetch(tbWmsMaterialDeliveryLineSaveParam.getLocationNo(),tbWmsMaterialDeliveryLineSaveParam.getId());
        }*/
        this.iTbWmsMaterialReceiptLineService.generateWarehouseArForDelivery(outids);
        return 0;
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public TWmsDeliveryPickupPlanVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<TWmsDeliveryPickupPlanVo> findAll(TWmsDeliveryPickupPlanSearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TWmsDeliveryPickupPlanVo> findAll(TWmsDeliveryPickupPlanSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

}
